package cn.edu.scau.cmi.linhongxin.client;


import cn.edu.scau.cmi.linhongxin.factory.*;
import cn.edu.scau.cmi.linhongxin.simpleFactoryMethod.MeatFactory;
import cn.edu.scau.cmi.linhongxin.simpleFactoryMethod.Produce;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class FactoryClient extends Application{
	@FXML
	private AnchorPane anchorPane;
	@FXML
	private TextArea creatOutput;
	@FXML
	private Button button;
	@FXML
	private ChoiceBox<String> choiceBox;
	private String nowMeat;
	private final String[] chooseType = {"pork", "beef", "chicken"};
	@FXML
	public  void initialize() {	
		
		ObservableList<String> list = FXCollections.observableArrayList(chooseType);
		nowMeat = chooseType[0];
		choiceBox.setItems(list);
		choiceBox.setTooltip(new Tooltip("选择肉类"));
		choiceBox.getSelectionModel().selectFirst();
		choiceBox.getSelectionModel().selectedIndexProperty().addListener(
			new ChangeListener<Number>() {
				@Override
				public void changed(ObservableValue<? extends Number> observable, Number oldValue,
						Number newValue) {
					nowMeat = chooseType[newValue.intValue()];
				}
			});
		listenButton();
	}
	public void listenButton() {
		
		button.setOnMouseClicked(new EventHandler<MouseEvent>() {			
			@Override
			public void handle(MouseEvent arg) {
/* 				if(nowMeat.compareTo(chooseType[0]) == 0) {
//					creatOutput.setText("dvdvd");
					return;
				}*/
				creatOutput.clear();
/*				Factory porkMeatFactory=new PorkMeatFactory();
				MeatFactory pork=porkMeatFactory.produce();
				pork.getMeatInfo();
				
				MeatFactory factory = null;; 
				if(nowMeat.compareTo(chooseType[1]) == 0) {
					factory = new PorkMeatFactory();
				} else if(nowMeat.compareTo(chooseType[2]) == 0) {
					factory = new BeefMeatFactory();
				} else if(nowMeat.compareTo(chooseType[3]) == 0) {
					factory = new ChickenMeatFactory();
				}
				
				MeatFactory pork =Produce.produce("pork");
//				pork.getMeatInfo();//生产pork
				MeatFactory instance = Produce.produce(nowMeat);*/
				Factory factory=null;
//				System.out.println(instance.getMeatInfo());
//				System.out.println(creatOutput);
				if(nowMeat.compareTo(chooseType[0]) == 0) {
					factory = new PorkMeatFactory();
				} else if(nowMeat.compareTo(chooseType[1]) == 0) {
					factory = new BeefMeatFactory();
				} else if(nowMeat.compareTo(chooseType[2]) == 0) {
					factory = new ChickenMeatFactory();
				}
//				System.out.println(factory.getFactoryInfo());
//				System.out.println(creatOutput);
				creatOutput.appendText(factory.getFactoryInfo()+'\n');
				
			}
		});
		
	}
	private Stage PrimaryStage;
	private AnchorPane rootLayout;
	@Override
	public void start(Stage primaryStage) {
		try {
			this.PrimaryStage = primaryStage;
			this.PrimaryStage.setTitle("工厂模式");			
			initRootLayout();		
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void initRootLayout() {
		try {
			FXMLLoader loader = new FXMLLoader();
			loader.setLocation(FactoryClient.class.getResource("/cn/edu/scau/cmi/linhongxin/factory/FactoryStage.fxml"));
			rootLayout = (AnchorPane) loader.load();		
			Scene scene = new Scene(rootLayout,900,500);
			PrimaryStage.setScene(scene);
			PrimaryStage.show();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
/*	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Factory porkMeatFactory=new PorkMeatFactory();
		MeatFactory pork=porkMeatFactory.produce();
		pork.getMeatInfo();
		Factory beefMeatFactory=new BeefMeatFactory();
		MeatFactory beef=beefMeatFactory.produce();
		beef.getMeatInfo();
		Factory chickenMeatFactory=new ChickenMeatFactory();
		MeatFactory chicken=chickenMeatFactory.produce();
		chicken.getMeatInfo();	
	}*/

}
